# -*- coding: utf-8 -*-
# @Time    : 2018/3/7 11:32
# @Author  : Deyu.Tian
# @Site    :
# @File    : config.py
# @Software: PyCharm Community Edition
from geotiff_tools import *
from config import *
import numpy as np
from sklearn.preprocessing import OneHotEncoder

def onehot2tiff(imggt):
    categri_onehot_features = np.load("{}/categri_onehot_features.npy".format(catgriDir))
    print(categri_onehot_features.shape)

    for i in range(len(categri_onehot_features)):
        array2rasterUTM("{}/categri_onehot_features_{}.tif".format(onehotDir, i+1), imggt, categri_onehot_features[i])
    pass
def onehot_encoding_categorical(categri_features, imggt):
    enc = OneHotEncoder()
    samples = []
    for i in range(3508):
        for j in range(2290):
            samples.append(categri_features[:, i, j])
            pass
    enc.fit(samples)
    #print(enc.transform(categri_features[:, 1000, 1000].reshape(1, -1)).toarray())
    categri_onehot =  np.zeros((31, 3508, 2990))
    print(categri_onehot.shape)

    for i in range(3508):
        for j in range(2290):
            categri_onehot[:, i, j] = enc.transform(categri_features[:, i, j].reshape(1, -1)).toarray()
            #print(categri_onehot[:, i, j])

    np.save("{}/categri_onehot_features.npy".format(catgriDir), categri_onehot)
    #array2rasterUTM("{}/categri_onehot_features.tif".format(catgriDir), imggt, categri_onehot)
    pass


def integerCodingSoiltype(soil, imggt):
    """
    [ 11173.-> 5  11238.-> 4  11307.->3  11413.->2  11765.->1  11927.->0  11930.->0  99999.->0]
    :param soiltype:
    :param imggt:
    :return:
    """
    for i in range(soil.shape[0]):
        for j in range(soil.shape[1]):
            if soil[i, j] == 99999 or soil[i, j] == 11930 or soil[i, j] == 11927:
                soil[i, j] = 0
            if soil[i, j] == 11765:
                soil[i, j] = 1
            if soil[i, j] == 11413:
                soil[i, j] = 2
            if soil[i, j] == 11307:
                soil[i, j] = 3
            if soil[i, j] == 11238:
                soil[i, j] = 4
            if soil[i, j] == 11173:
                soil[i, j] = 5
    array2rasterUTM("{}/soiltype_intercode.tif".format(catgriDir), imggt, soil)
    pass
def integerCodingLandforms(landforms, imggt):
    """
    [   1.->8   17.->7   33.->6   49.->5   65.->4   81.->3   97.->2  113.->1  255.->0]
    :param landformstype:
    :param imggt:
    :return:
    """
    for i in range(landforms.shape[0]):
        for j in range(landforms.shape[1]):
            if landforms[i, j] == 1:
                landforms[i, j] = 8
            if landforms[i, j] == 17:
                landforms[i, j] = 7
            if landforms[i, j] == 33:
                landforms[i, j] = 6
            if landforms[i, j] == 49:
                landforms[i, j] = 5
            if landforms[i, j] == 65:
                landforms[i, j] = 4
            if landforms[i, j] == 81:
                landforms[i, j] = 3
            if landforms[i, j] == 97:
                landforms[i, j] = 2
            if landforms[i, j] == 113:
                landforms[i, j] = 1
            if landforms[i, j] == 255:
                landforms[i, j] = 0
    array2rasterUTM("{}/landforms_intercode.tif".format(catgriDir), imggt, landforms)
    pass


if __name__ == '__main__':
    imggt = read_tif_metadata("{}/table_4_5_hygeos_terrain.tif".format(terrainDir))

    # stacked_categri_features = read_tiff("{}/stacked_categri_features.tif".format(catgriDir))
    #
    # onehot_encoding_categorical(stacked_categri_features, imggt)
    onehot2tiff(imggt)